--事件优先队列

local EventQueue  = class("EventQueue")

function EventQueue:ctor()
	self.queue = {}

end

function EventQueue:clear()
	self.queue = {}

end

function EventQueue:insert(event)
	table.insert(self.queue,event)
end

function EventQueue:remove(user)
	for i = #self.queue,1,-1 do
		if self.queue[i].user == user and self.queue[i].type ~= self.queue[i].PAO then
			table.remove(self.queue,i)
		end
	end
end

function EventQueue:sort()
	function queue_sort(event1,event2)
		if event1.type ==  event2.type then
			return event1.priority < event2.priority
		else
			return event1.type > event2.type
		end
	end
	table.sort(self.queue,queue_sort)
end

function EventQueue:confirm(user,tp,message)	
	for i=#self.queue,1 ,-1 do		
		if user == self.queue[i].user and tp == self.queue[i].type then
			self.queue[i].message = self.queue[i].message or message
			self.queue[i].confirm = true
		end
		--确认后删除用户可选的操作
		if user == self.queue[i].user and tp ~= self.queue[i].type then
			table.remove(self.queue,i)
		end
	end	
end

function EventQueue:pop_first_event()
	return table.remove(self.queue,1)
end

function EventQueue:get_first_event()
	return self.queue[1]
end

function EventQueue:get_first_confirmed_event()
	for i,v in ipairs(self.queue) do
		--如果第一个是跑,那么返回跑
		if i == 1 and v.type == self.queue[i].PAO then
			v.confirm = true
			return v
		end
		if v:is_confirm() then
			return v
		end
	end
end

function EventQueue:has(user,tp)
	for i,v in ipairs(self.queue) do
		if v.user == user and v.type == tp then
			return true
		end
	end
	return false
end

function EventQueue:size()
	return #self.queue
end


return EventQueue